//
//  TeherapeuticTouch.swift
//  HIextractLicve
//
//  Created by HIextractLicve on 2024/12/6.
//

import UIKit
import ProgressHUD


/// Ai选择
class TeherapeuticTouch: UIViewController {
    @IBOutlet weak var ainameEntertefHTLC: UITextField!
    
    @IBOutlet weak var namedscfvHTLC: UILabel!
    @IBOutlet weak var mainGunHTLC: UIScrollView!
    
    @IBAction func Funrightnoweback(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
  
    override func viewDidLoad() {
        super.viewDidLoad()

        ainameEntertefHTLC.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12)
            make.trailing.equalToSuperview().inset(19)
            make.width.equalTo(UIScreen.main.bounds.width - 12 - 19)
            make.height.equalTo(52)
            make.bottom.equalToSuperview().offset(-150 - bommbsafeHeitHTL )
            make.top.equalTo(namedscfvHTLC.snp.bottom).offset(23)
            
        }
        mainGunHTLC.isScrollEnabled = true
        let burrrtonf11 = view.viewWithTag(11) as? UIButton
        let burrrtonf12 = view.viewWithTag(12) as? UIButton
        let burrrtonf13 = view.viewWithTag(13) as? UIButton
        
        burrrtonf11?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChatStyleTag.0
        burrrtonf12?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChatStyleTag.1
        burrrtonf13?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChatStyleTag.2
        
        let burrrtonf21 = view.viewWithTag(21) as? UIButton
        let burrrtonf22 = view.viewWithTag(22) as? UIButton
        let burrrtonf23 = view.viewWithTag(23) as? UIButton
        let burrrtonf24 = view.viewWithTag(24) as? UIButton
        
        burrrtonf21?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChattargetTag.0
        burrrtonf22?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChattargetTag.1
        burrrtonf23?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChattargetTag.2
        burrrtonf24?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChattargetTag.3
       
        
        let burrrtonf31 = view.viewWithTag(31) as? UIButton
        let burrrtonf32 = view.viewWithTag(32) as? UIButton
        let burrrtonf33 = view.viewWithTag(33) as? UIButton
        let burrrtonf34 = view.viewWithTag(34) as? UIButton
       
        burrrtonf31?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.CurrentemotionsTag.0
        burrrtonf32?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.CurrentemotionsTag.1
        burrrtonf33?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.CurrentemotionsTag.2
        burrrtonf34?.isSelected =  HTLCAllDealRelaxing.Relaxing.setChatAiHTL.CurrentemotionsTag.3
       
        
    }

    var styleChatAIHTLC = (true,false,false)
    @IBAction func stylechatTHLCClick(_ sender: UIButton) {
        let burrrtonf11 = view.viewWithTag(11) as? UIButton
        let burrrtonf12 = view.viewWithTag(12) as? UIButton
        let burrrtonf13 = view.viewWithTag(13) as? UIButton
        
        
          
        if sender.tag == 11 {
            burrrtonf11?.isSelected = true
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = false
            styleChatAIHTLC = (true,false,false)
        }
        
        if sender.tag == 12 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = true
            burrrtonf13?.isSelected = false
            styleChatAIHTLC = (false,true,false)
        }
        
        if sender.tag == 13 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = true
            styleChatAIHTLC = (false,false,true)
            
        }
    }
    
    var targetChatAIHTLC = (true,false,false,false)
    
    @IBAction func targetchatTHLCClick(_ sender: UIButton) {
        let burrrtonf11 = view.viewWithTag(21) as? UIButton
        let burrrtonf12 = view.viewWithTag(22) as? UIButton
        let burrrtonf13 = view.viewWithTag(23) as? UIButton
        let burrrtonf14 = view.viewWithTag(24) as? UIButton
        
        
          
        if sender.tag == 21 {
            burrrtonf11?.isSelected = true
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = false
            burrrtonf14?.isSelected = false
            targetChatAIHTLC = (true,false,false,false)
            
        }
        
        if sender.tag == 22 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = true
            burrrtonf13?.isSelected = false
            burrrtonf14?.isSelected = false
            targetChatAIHTLC = (false,true,false,false)
        }
        
        if sender.tag == 23 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = true
            
            burrrtonf14?.isSelected = false
           
            targetChatAIHTLC = (false,false,true,false)
            
        }
        if sender.tag == 24 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = false
            burrrtonf14?.isSelected = true
            targetChatAIHTLC = (false,false,false,true)
        }
        
    }
    
    var emotiontChatAIHTLC = (true,false,false,false)
    @IBAction func emotionchatTHLCClick(_ sender: UIButton) {
        let burrrtonf11 = view.viewWithTag(31) as? UIButton
        let burrrtonf12 = view.viewWithTag(32) as? UIButton
        let burrrtonf13 = view.viewWithTag(33) as? UIButton
        let burrrtonf14 = view.viewWithTag(34) as? UIButton
        
        
          
        if sender.tag == 31 {
            burrrtonf11?.isSelected = true
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = false
            burrrtonf14?.isSelected = false
            emotiontChatAIHTLC = (true,false,false,false)
        }
        
        if sender.tag == 32 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = true
            burrrtonf13?.isSelected = false
            burrrtonf14?.isSelected = false
            emotiontChatAIHTLC = (false,true,false,false)
        }
        
        if sender.tag == 33 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = true
            burrrtonf14?.isSelected = false
            emotiontChatAIHTLC = (false,false,true,false)
            
        }
        if sender.tag == 34 {
            burrrtonf11?.isSelected = false
            burrrtonf12?.isSelected = false
            burrrtonf13?.isSelected = false
            burrrtonf14?.isSelected = true
            emotiontChatAIHTLC = (false,false,false,true)
            
        }
        
    }
    
//    {
    //
    //        if ainameEntertefHTLC.text == nil || ainameEntertefHTLC.text?.count == 0 {
    //            ProgressHUD.banner(HTLROOOMMeasg.BinrwetioBase64("Q2hhdGJvdCBOYW1lIGlzIGVtcHR5ISEh"), nil, delay: 2.0)
    //            return
    //        }
    //        let customized = "1customized0".components(separatedBy: "customized")
    //        var banlanceHTLC =  Int(HTLCAllDealRelaxing.Relaxing.signinUserDataHTL["HTLCcoin"] ?? customized[1]) ?? 0
    //
    //        if banlanceHTLC < 100 {
    //
    //            let alertv = UIAlertController(title:HTLROOOMMeasg.BinrwetioBase64("SW5zdWZmaWNpZW50IGNvaW5z") , message: HTLROOOMMeasg.BinrwetioBase64("Q3JlYXRpbmcgYSBjaGF0Ym90IGZvciB0aGUgZmlyc3QgdGltZSBjb3N0cyAxMDAgY29pbnMsIHRoZSBmaXJzdCBjaGF0IGlzIGZyZWUsIGFuZCAxMCBjb2lucyB3aWxsIGJlIGRlZHVjdGVkIGZvciBlYWNoIHN1YnNlcXVlbnQgY2hhdCE="), preferredStyle: .alert)
    //            let scdtion = UIAlertAction(title: HTLROOOMMeasg.BinrwetioBase64("R28gYnV5"), style: .default) { scd in
    //                self.navigationController?.pushViewController(MindBodyConnection.init(), animated: true)
    //            }
    //            alertv.addAction(scdtion)
    //            alertv.addAction(UIAlertAction(title:HTLROOOMMeasg.BinrwetioBase64("Q2FuY2Vs") , style: .default))
    //            self.present(alertv, animated: true)
    //
    //            return
    //        }
    //
    //        banlanceHTLC -= 100
    //        HTLCAllDealRelaxing.Relaxing.signinUserDataHTL["HTLCcoin"] = "\(banlanceHTLC)"
    //
    //        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.aiChatNameHTL = ainameEntertefHTLC.text
    //
    //        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChattargetTag = targetChatAIHTLC
    //        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChatStyleTag = styleChatAIHTLC
    //
    //        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.CurrentemotionsTag = emotiontChatAIHTLC
    //        ProgressHUD.succeed(HTLROOOMMeasg.BinrwetioBase64("Q3JlYXRlZCBzdWNjZXNzZnVsbHksIGxldCdzIHN0YXJ0IGNoYXR0aW5nIG5vdyE="), delay: 2.5)
    //        self.navigationController?.pushViewController(StorytellingHealing(), animated: true)
    //
    //    }
        
        @IBAction func ckiosetupAIONe(_ sender: Any) {
        guard let aiName = ainameEntertefHTLC.text, !aiName.isEmpty else {
            showAlertForEmptyName()
            return
        }
        
        var balance = getUserBalance()
        
        if balance < 100 {
            showInsufficientFundsAlert()
            return
        }
        
        updateUserBalance(&balance)
        saveAISettings(with: aiName)
        showSuccessMessage()
        navigateToStorytellingPage()
    }

    private func showAlertForEmptyName() {
        ProgressHUD.banner(HTLROOOMMeasg.BinrwetioBase64("Q2hhdGJvdCBOYW1lIGlzIGVtcHR5ISEh"), nil, delay: 2.0)
    }

    private func getUserBalance() -> Int {
        let customized = "1customized0".components(separatedBy: "customized")
        return Int(HTLCAllDealRelaxing.Relaxing.signinUserDataHTL["HTLCcoin"] ?? customized[1]) ?? 0
    }

    private func showInsufficientFundsAlert() {
        let alert = UIAlertController(
            title: HTLROOOMMeasg.BinrwetioBase64("SW5zdWZmaWNpZW50IGNvaW5z"),
            message: HTLROOOMMeasg.BinrwetioBase64("Q3JlYXRpbmcgYSBjaGF0Ym90IGZvciB0aGUgZmlyc3QgdGltZSBjb3N0cyAxMDAgY29pbnMsIHRoZSBmaXJzdCBjaGF0IGlzIGZyZWUsIGFuZCAxMCBjb2lucyB3aWxsIGJlIGRlZHVjdGVkIGZvciBlYWNoIHN1YnNlcXVlbnQgY2hhdCE="),
            preferredStyle: .alert
        )
        
        let action = UIAlertAction(title: HTLROOOMMeasg.BinrwetioBase64("R28gYnV5"), style: .default) { _ in
            self.navigationController?.pushViewController(MindBodyConnection(), animated: true)
        }
        
        alert.addAction(action)
        alert.addAction(UIAlertAction(title: HTLROOOMMeasg.BinrwetioBase64("Q2FuY2Vs"), style: .default))
        self.present(alert, animated: true)
    }

    private func updateUserBalance(_ balance: inout Int) {
        balance -= 100
        HTLCAllDealRelaxing.Relaxing.signinUserDataHTL["HTLCcoin"] = "\(balance)"
    }

    private func saveAISettings(with aiName: String) {
        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.aiChatNameHTL = aiName
        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChattargetTag = targetChatAIHTLC
        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.ChatStyleTag = styleChatAIHTLC
        HTLCAllDealRelaxing.Relaxing.setChatAiHTL.CurrentemotionsTag = emotiontChatAIHTLC
    }

    private func showSuccessMessage() {
        ProgressHUD.succeed(HTLROOOMMeasg.BinrwetioBase64("Q3JlYXRlZCBzdWNjZXNzZnVsbHksIGxldCdzIHN0YXJ0IGNoYXR0aW5nIG5vdyE="), delay: 2.5)
    }

    private func navigateToStorytellingPage() {
        self.navigationController?.pushViewController(StorytellingHealing(), animated: true)
    }

}
